package com.bj.disc.util

import android.graphics.Rect
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView

class GridSpaceItemDecoration(//横条目数量
    private val mSpanCount: Int, //行间距
    private var mRowSpacing: Int, columnSpacing: Int
) :
    RecyclerView.ItemDecoration() {
    private val TAG = "GridSpaceItemDecoration"
    private val mColumnSpacing // 列间距
            : Int

    override fun getItemOffsets(
        @NonNull outRect: Rect,
        @NonNull view: View,
        @NonNull parent: RecyclerView,
        @NonNull state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view) // 获取view 在adapter中的位置。
        val column = position % mSpanCount // view 所在的列
        outRect.left = column * mColumnSpacing / mSpanCount // column * (列间距 * (1f / 列数))
        outRect.right =
            mColumnSpacing - (column + 1) * mColumnSpacing / mSpanCount // 列间距 - (column + 1) * (列间距 * (1f /列数))
        // 如果position > 行数，说明不是在第一行，则不指定行高，其他行的上间距为 top=mRowSpacing
    }

    /**
     * @param spanCount     列数
     * @param rowSpacing    行间距
     * @param columnSpacing 列间距
     */
    init {
        mRowSpacing = mRowSpacing
        mColumnSpacing = columnSpacing
    }
}